package com.poi.tl.sample.quickstart.app.plugins.custom;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Numberings;
import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.render.WhereDelegate;
import com.deepoove.poi.util.TableTools;
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import com.poi.tl.sample.quickstart.consts.AppConsts;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.core.io.ClassPathResource;

public class _1CustomPluginExampleExport {

  public static void main(String[] args) throws Exception {
    ClassPathResource templateFile = new ClassPathResource(AppConsts.PLUGIN_TEMPLATE_CLASS_PATH + "_1custom_plugin_template.docx");
    ClassPathResource sayiImg = new ClassPathResource(AppConsts.IMG_CLASS_PATH + "sayi.png");
    String sayiImgImgPath = sayiImg.getFile().getPath();
    String outFile = AppConsts.EXPORT_PATH + "_1custom_plugin_template_output.docx";

    // where绑定policy
    Configure config = Configure.builder().bind("sea", new AbstractRenderPolicy<String>() {
      @Override
      public void doRender(RenderContext<String> context) throws Exception {
        // anywhere
        XWPFRun where = context.getWhere();
        // anything
        String thing = context.getThing();
        // do 文本
        where.setText(thing, 0);
      }
    }).bind("sea_img", new AbstractRenderPolicy<String>() {
      @Override
      public void doRender(RenderContext<String> context) throws Exception {
        // anywhere delegate
        WhereDelegate where = context.getWhereDelegate();
        // any thing
        String thing = context.getThing();
        // do 图片
        FileInputStream stream = null;
        try {
          stream = new FileInputStream(thing);
          where.addPicture(stream, XWPFDocument.PICTURE_TYPE_JPEG, 400, 450);
        } finally {
          IOUtils.closeQuietly(stream);
        }
        // clear
        clearPlaceholder(context, false);
      }
    }).bind("sea_feature", new AbstractRenderPolicy<List<String>>() {
      @Override
      public void doRender(RenderContext<List<String>> context) throws Exception {
        // anywhere delegate
        WhereDelegate where = context.getWhereDelegate();
        // anything
        List<String> thing = context.getThing();
        // do 列表
        where.renderNumbering(Numberings.of(thing.toArray(new String[] {})).create());
        // clear
        clearPlaceholder(context, true);
      }
    }).bind("sea_location", new AbstractRenderPolicy<List<String>>() {
      @Override
      public void doRender(RenderContext<List<String>> context) throws Exception {
        // anywhere
        XWPFRun where = context.getWhere();

        // anything
        List<String> thing = context.getThing();

        // do 表格
        int row = thing.size() + 1, col = 2;
        BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(where);
        XWPFTable table = bodyContainer.insertNewTable(where, row, col);
        TableTools.widthTable(table, 14.65f, col);
        TableTools.borderTable(table, 4);

        table.getRow(0).getCell(0).setText("编号");
        table.getRow(0).getCell(1).setText("地点信息");

        for (int i = 0; i < thing.size(); i++) {
          table.getRow(i + 1).getCell(0).setText("No." + i);
          table.getRow(i + 1).getCell(1).setText(thing.get(i));
        }

        // clear
        clearPlaceholder(context, true);
      }
    }).build();

    // 初始化where的数据
    HashMap<String, Object> args1 = new HashMap<String, Object>();
    args1.put("sea", "Hello, world!");
    args1.put("sea_img", sayiImgImgPath);
    args1.put("sea_feature", Arrays.asList("面朝大海春暖花开", "今朝有酒今朝醉"));
    args1.put("sea_location", Arrays.asList("日落：日落山花红四海", "花海：你想要的都在这里"));

    // 一行代码
    XWPFTemplate.compile(templateFile.getInputStream(), config).render(args1).writeToFile(outFile);
  }

}
